package com.ruoyi.task.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.buildingtask.domain.SysBuildingsTask;
import com.ruoyi.buildingtask.service.ISysBuildingsTaskService;
import com.ruoyi.household.domain.SysHousehold;
import com.ruoyi.household.service.ISysHouseholdService;
import com.ruoyi.usertask.domain.SysUserTask;
import com.ruoyi.usertask.service.ISysUserTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.task.mapper.SysTaskMapper;
import com.ruoyi.task.domain.SysTask;
import com.ruoyi.task.service.ISysTaskService;

/**
 * 任务表Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-16
 */
@Service
public class SysTaskServiceImpl implements ISysTaskService 
{
    @Autowired
    private SysTaskMapper sysTaskMapper;

    @Autowired
    private ISysBuildingsTaskService sysBuildingsTaskService;
    @Autowired
    private ISysUserTaskService sysUserTaskService;

    @Autowired
    private ISysHouseholdService householdService;

    /**
     * 查询任务表
     * 
     * @param id 任务表主键
     * @return 任务表
     */
    @Override
    public SysTask selectSysTaskById(Integer id)
    {
        return sysTaskMapper.selectSysTaskById(id);
    }

    /**
     * 查询任务表列表
     * 
     * @param sysTask 任务表
     * @return 任务表
     */
    @Override
    public List<SysTask> selectSysTaskList(SysTask sysTask)
    {
        return sysTaskMapper.selectSysTaskList(sysTask);
    }

    /**
     * 新增任务表
     * 
     * @param sysTask 任务表
     * @return 结果
     */
    @Override
    public int insertSysTask(SysTask sysTask)
    {
        //发布任务时，添加到楼栋任务和住户任务
        SysBuildingsTask sysBuildingsTask = new SysBuildingsTask();
        String buildingName = sysTask.getBuildingName();
        Date endTime = sysTask.getEndTime();
        Date beginTime = sysTask.getBeginTime();
        String address = sysTask.getAddress();
        Integer buildingId = sysTask.getBuildingId();
        sysBuildingsTask.setAddress(address);
        sysBuildingsTask.setCommunityId(buildingId);
        sysBuildingsTask.setBeginTime(beginTime);
        sysBuildingsTask.setEndTime(endTime);
        sysBuildingsTask.setTaskName(buildingName+beginTime+"开始做核酸");
        sysBuildingsTask.setFinishNumber(sysTask.getFinishNumber());
        sysBuildingsTask.setTaskNumber(sysTask.getTaskNumber());
        sysBuildingsTaskService.insertSysBuildingsTask(sysBuildingsTask);
        //添加到住户任务表,communityId作为楼栋id

        // 查询该楼栋的用户
        //
        SysUserTask sysUserTask = new SysUserTask();
        sysUserTask.setBuildingId(buildingId);
        sysUserTask.setTaskName(buildingName+beginTime+"开始做核酸");
        sysUserTask.setStatus(1);
        sysUserTask.setBeginTime(beginTime);
        sysUserTask.setEndTime(beginTime);
        sysUserTask.setAddress(address);
        //获取用户名字
        SysHousehold sysHousehold= householdService.selectName(buildingName);
        String name = sysHousehold.getName();
        sysUserTask.setName(name);
        sysUserTaskService.insertSysUserTask(sysUserTask);

        return sysTaskMapper.insertSysTask(sysTask);
    }

    /**
     * 修改任务表
     * 
     * @param sysTask 任务表
     * @return 结果
     */
    @Override
    public int updateSysTask(SysTask sysTask)
    {
        return sysTaskMapper.updateSysTask(sysTask);
    }

    /**
     * 批量删除任务表
     * 
     * @param ids 需要删除的任务表主键
     * @return 结果
     */
    @Override
    public int deleteSysTaskByIds(Integer[] ids)
    {
        return sysTaskMapper.deleteSysTaskByIds(ids);
    }

    /**
     * 删除任务表信息
     * 
     * @param id 任务表主键
     * @return 结果
     */
    @Override
    public int deleteSysTaskById(Integer id)
    {
        return sysTaskMapper.deleteSysTaskById(id);
    }
}
